#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include "cJSON/cJSON.h" 

//
//
//打开文件
FILE * open_file(char * path)
{
	FILE * fp=fopen(path,"w+");
	if(NULL==fp)
	{
		return NULL;
	}
	return fp;
}


///获得文件的大小
int  file_size(char * path)
{
	FILE * fp=fopen(path,"r");
	if(NULL==fp)
	{
		perror("open error");
		return -1;
	}


	fseek(fp,0,SEEK_END);
	   int size= ftell(fp);
	   	return size;
			


}
int main(void)
{
	//open file
	FILE * fp=open_file("1.txt");
	// 1初始化
	//CURL * curl=curl_easy_init();
	///
	CURL * curl=curl_easy_init();

	//2设置要访问的地址
	curl_easy_setopt(curl,CURLOPT_URL,"http://www.nmc.cn/f/rest/real/57171");

	//3.
	curl_easy_setopt(curl,CURLOPT_WRITEDATA,fp);
	

	//3要访问的地址
	curl_easy_perform(curl);
	//4.清除curl
	curl_easy_cleanup(curl);

	
	fclose(fp);
	
	int size=file_size("1.txt");
	fseek(fp,0,SEEK_SET);
	
	char * jsonstr=(char *)malloc(size);
	fread(jsonstr,1,size,fp);
    fclose(fp);
	//解析json字符串
	cJSON * json=cJSON_Parse(jsonstr);
	if(NULL==json)
	{
		const char * err_pos=cJSON_GetErrorPtr();
		if(NULL!=err_pos)
		{
			fprintf(stderr,"Error before:%s\n",err_pos);
		}
	}
	
	
	
	cJSON * weather= cJSON_GetObjectItemCaseSensitive(monitor_json, "weather");
	
	cJSON * temperature=cJSON_GetObjectItemCaseSensitive(weather,"temperature");
	
	
	printf("当前温度:%f",comfirmed->valuefloat);
	
	//释放json数据结构占用的内存
	cJSON_free(json);

}

